package Views.TableModels;

import Models.*;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 * @author Morten
 */
public class SaleTableModel extends DefaultTableModel
{
	ArrayList<Sale> sales;

	public SaleTableModel()
	{
		this.sales = new ArrayList<Sale>();
	}

	public SaleTableModel(ArrayList<Sale> sales)
	{
		this.sales = sales;
	}

		@Override
	public boolean isCellEditable(int row, int column)
	{
		return false;
	}

	public void setData(ArrayList<Sale> machines)
	{
		this.sales = machines;
		fireTableDataChanged();
	}

	public ArrayList<Sale> getData()
	{
		return this.sales;
	}

	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount()
	{
		if(this.sales == null)
		{
			return 0;
		}
		else
		{
			return this.sales.size();
		}
	}

	@Override
	public String getColumnName(int column) {
		String columnName = "";
		switch(column)
		{
			case 0:
				columnName = "ID";
				break;
			case 1:
				columnName = "Kunde";
				break;
			case 2:
				columnName = "Dato";
				break;

		}
		return columnName;
	}



	@Override
	public Object getValueAt(int row, int column)
	{
		Object value = null;
		switch(column)
		{
			case 0:
				value = this.sales.get(row).getId();
				break;
			case 1:
				if(this.sales.get(row).getCustomer() instanceof PrivateCustomer)
				{
					PrivateCustomer pCustomer = (PrivateCustomer)sales.get(row).getCustomer();
					value = pCustomer.getName();
				}
				else
				{
					BusinessCustomer bCustomer = (BusinessCustomer)sales.get(row).getCustomer();
					value = bCustomer.getCompanyName();
				}
				break;
			case 2:
				value = byggemarked.Tools.makeNiceStringFromDate(this.sales.get(row).getDate());
				break;

		}

		return value;
	}
}